package StringHandling;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringStripper {
	private String substituionName = "Rellyk";

	public String stripCombatName(String aName) {
		String player = null;
		String stringToRemove = "(Critical)";
		String replacementValue = "";
		String result = aName.replace(stringToRemove + " ", replacementValue);
		Matcher matcher = Pattern.compile("(?<=\\]\\s{1})(\\w*\\,\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)|(?<=\\]\\s{1})(\\w*\\,\\s{1}\\w*\\s{1}\\w*)|(?<=\\]\\s{1})(\\w[a-zA-Z]*)\\s{1}(\\w[a-zA-Z]*)\\s{1}(\\w[a-zA-Z]*)(?='s)|(?<=\\]\\s{1})(\\w[a-zA-Z]*)\\s{1}(\\w[a-zA-Z]*)(?='s)|(?<=\\]\\s{1})(\\w[a-zA-Z]*)(?='s)|(Your)|(?<=\\]\\s{1})(\\w*\\-\\w*\\,\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)(?=\\'s\\s{1}Attack)|(?<=\\]\\s{1})(\\w*\\,\\s{1}\\w*\\-\\w*\\-\\w*)").matcher(result);
		while (matcher.find()) {
			if (aName.contains("Your")) {
				player = substituionName;
			} else {
				player = matcher.group().trim();
			}
		}
		return player;
	}

	public int stripTime(String aTime) {

		int timeTotal = 0;
		Matcher matcher = Pattern.compile("(?<=\\[)([0-2][0-9])\\:([0-6][0-9])\\:([0-6][0-9])(?=\\])").matcher(aTime);

		while(matcher.find()) {
			int hours = Integer.parseInt(matcher.group(1)) * 3600;
			int mins = Integer.parseInt(matcher.group(2)) * 60;
			int secs = Integer.parseInt(matcher.group(3));

			timeTotal =  hours + mins + secs;
		}
		return timeTotal;
	}

	public int stripDamage(String aDamage) {
		int damage = 0;
		Matcher matcher = Pattern.compile("\\s{1}\\d{1,5}\\s{1}").matcher(aDamage);

		while (matcher.find()) {
			damage = Integer.parseInt(matcher.group().trim());
		}
		return damage;
	}

	public String stripCombatTargetName(String aBoss) {
		String aBossName = "";
		Matcher matcher = Pattern.compile("(?<=\\)\\s{1})(\\w*\\,\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)|(?<=\\)\\s{1})(\\w*\\,\\s{1}\\w*\\s{1}\\w*)|(?<=\\)\\s{1})(\\w*)\\s{1}(?=for)|(?<=\\)\\s{1})(\\w*\\s{1}\\w*)\\s{1}(?=for)|(?<=\\)\\s{1})(\\w*\\-\\w*\\,\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)\\s{1}(?=for)|(?<=\\)\\s{1})(\\w*\\,\\s{1}\\w*\\-\\w*\\-\\w*)\\s{1}(?=for)|(?<=\\)\\s{1})(\\w*\\s{1}\\w*\\s{1}\\w*)\\s{1}(?=for)").matcher(aBoss);
		while (matcher.find()) {
			if (aBoss.contains("you")) {
				aBossName = substituionName;
			} else {
				aBossName = matcher.group().trim();
			}
		}
		return aBossName;
	}

	public String stripCombatAbility(String aString) {
		String abilityName = null;
		Matcher matcher = Pattern.compile("(?<=\\'[s]|(Your)\\s{1}).*(?=hits)").matcher(aString);

		while (matcher.find()) {
			abilityName = matcher.group().trim();
		}
		return abilityName;
	}

	public String stripSpecialHit(String anEvent) {
		StringBuilder str = new StringBuilder();
		Matcher matcher = Pattern.compile("(?<=\\()Blocked|Glancing|Critical|Penetrated(?=\\))").matcher(anEvent);
		while(matcher.find()) {
			str.append(matcher.group().trim() + " ");
		}

		String result = str.toString().trim();
		return result;
	}

	//DEATH
	
	public String stripDeathName(String aDeathEvent) {

		String aDeath = "";
		Matcher matcher = Pattern.compile("(?<=\\]\\s{1})(\\w*)\\s{1}(?=died.)|(?<=\\]\\s{1})(\\w*\\s{1}\\w*)\\s{1}(?=died.)|(?<=\\]\\s{1})(\\w*\\-\\w*\\,\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)\\s{1}(?=died.)|(?<=\\]\\s{1})(\\w*\\,\\s{1}\\w*\\-\\w*\\-\\w*)\\s{1}(?=died.)").matcher(aDeathEvent);
		while (matcher.find()) {
			aDeath = matcher.group().trim();
		}
		return aDeath;
	}		

	//HEALS
	public String stripHealTargetName(String aName) {
		String aTargetName = "";
		Matcher matcher = Pattern.compile("(?<=heals\\s{1})(\\w*\\s{1})*(?=for)|(?<=heals\\s{1})(\\w*\\,\\s{1}(\\w*\\s{1})*)(?=for)").matcher(aName);
		while (matcher.find()) {
			if (aName.contains("you")) {
				aTargetName = substituionName;
			} else {
				aTargetName = matcher.group().trim();
			}
		}
		return aTargetName;
	}

	public String stripHealAbility(String aString) {

		String abilityName = null;
		Matcher matcher = Pattern.compile("(?<=\\'[s]|(Your)\\s{1}).*(?=heals)").matcher(aString);

		while (matcher.find()) {
			abilityName = matcher.group().trim();

		}

		return abilityName;

	}

	public int stripHealAmount(String anAmount) {

		int damage = 0;
		Matcher matcher = Pattern.compile("\\s{1}\\d{1,5}(?=\\.)").matcher(anAmount);

		while (matcher.find()) {
			damage = Integer.parseInt(matcher.group().trim());
		}

		return damage;
	}

	//DEBUFF
	
	public String stripDebuffPlayerName(String anEvent) {
		String aTargetName = "";
		Matcher matcher = Pattern.compile("(?<=\\]\\s{1})(\\w*)\\s{1}(?=afflict)").matcher(anEvent);
		while (matcher.find()) {
			if (anEvent.contains("you")) {
				aTargetName = substituionName;
			} else {
				aTargetName = matcher.group().trim();
			}
		}
		return aTargetName;
	}
	
	public String stripDebuffTargetName(String anEvent) {
		String aTargetName = "";
		Matcher matcher = Pattern.compile("(?<=afflict\\s{1})((\\w*\\s{1})*)(?=with)").matcher(anEvent);
		while (matcher.find()) {
			if (anEvent.contains("you")) {
				aTargetName = substituionName;
			} else {
				aTargetName = matcher.group().trim();
			}
		}
		return aTargetName;
	}

	public String stripDebuffAbilityName(String aName) {

		String aDebuff = "";
		Matcher matcher = Pattern.compile("(?<=with)\\s*(.*)").matcher(aName);//checked
		while (matcher.find()) {
			aDebuff = matcher.group().trim();
		}
		return aDebuff;

	}
	
	//BUFF
	
	public String stripBuffPlayerName(String anEvent) {
		String player = null;
		String stringToRemove = "(Critical)";
		String replacementValue = "";
		String result = anEvent.replace(stringToRemove + " ", replacementValue);
		Matcher matcher = Pattern.compile("(?<=\\]\\s{1})(\\w*)(\\s{1}\\w*)*(?=gain)").matcher(result);
		while (matcher.find()) {
			if (anEvent.contains("Your")) {
				player = substituionName;
			} else {
				player = matcher.group().trim();
			}
		}
		return player;
	}
	
	public String stripBuffAbilityName(String aBuffEvent) {
		String aBuff = "";
		Matcher matcher = Pattern.compile("(?<=buff\\s{1})(\\w*\\s{1}\\-\\s{1}\\w*\\s{1}\\w*\\s{1}\\w*)|(?<=buff\\s{1})(\\w*)").matcher(aBuffEvent);
		while (matcher.find()) {
			aBuff = matcher.group().trim();
		}
		return aBuff;
	}
}
